<?php

namespace App\Http\Controllers\Backend;

use App\Shop;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShopController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $games=Shop::where('is_del','=',0)->paginate(10);
        return view('backend.shop.index')->with('games',$games);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('backend.shop.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $title=\Input::get('title');
        $content=\Input::get('content');
        $score=\Input::get('score');
        $num=\Input::get('num');
        $pic = $request->file('pic');
        $start_at=\Input::get('start_at');
        $end_at=\Input::get('end_at');
        $status=\Input::get('status');

        $rs='';
        if(isset($pic)){
            if ($pic->isValid()) {
                $clientName = $pic->getClientOriginalName();
                $entension = $pic -> getClientOriginalExtension();
                $newName = md5(date('ymdhis').$clientName).".".$entension;
                $rs=$pic -> move(base_path().'/public/uploads/',$newName);
            }
        }

        $Shop=new Shop();
        $Shop->title=$title;
        $Shop->content=$content;
        $Shop->score=$score;
        $Shop->num=$num;
        if($rs) {
            $Shop->pic = $newName;
        }
        $Shop->start_at=strtotime($start_at);
        $Shop->end_at=strtotime($end_at);
        $Shop->status=$status;
        $Shop->save();
        $data=array();
        $data['type']='s';
        $data['message']='添加成功';
        return redirect()->back()->with('message',$data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {
        $id=\Input::get('id');
        $shop=Shop::find($id);
        return view('backend.shop.edit')->with('shop',$shop);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id=\Input::get('id');
        $title=\Input::get('title');
        $content=\Input::get('content');
        $score=\Input::get('score');
        $num=\Input::get('num');
        $pic = $request->file('pic');
        $start_at=\Input::get('start_at');
        $end_at=\Input::get('end_at');
        $status=\Input::get('status');
        $Shop=Shop::find($id);


        $rs='';
        if(isset($pic)){
            if ($pic->isValid()) {
                $clientName = $pic->getClientOriginalName();
                $entension = $pic -> getClientOriginalExtension();
                $newName = md5(date('ymdhis').$clientName).".".$entension;
                $rs=$pic -> move(base_path().'/public/uploads/',$newName);
            }
        }

        $Shop->title=$title;
        $Shop->content=$content;
        $Shop->score=$score;
        $Shop->num=$num;
        if($rs) {
            $Shop->pic = $newName;
        }
        $Shop->start_at=strtotime($start_at);
        $Shop->end_at=strtotime($end_at);
        $Shop->status=$status;
        $Shop->save();
        $data=array();
        $data['type']='s';
        $data['message']='修改成功！';
        return redirect()->back()->with('message',$data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $id=\Input::get('id');
        $shop=Shop::find($id);
        $shop->is_del=1;
        $shop->save();
        $data=array();
        $data['type']='s';
        $data['message']='删除成功';
        return redirect()->back()->with('message',$data);
    }
}
